package com.chenyi.web.controller.system;

import com.chenyi.common.annotation.Log;
import com.chenyi.common.constant.UserConstants;
import com.chenyi.common.core.controller.BaseController;
import com.chenyi.common.core.domain.entity.SysDictType;
import com.chenyi.common.core.page.TableDataInfo;
import com.chenyi.common.enums.BusinessType;
import com.chenyi.common.exception.MessageException;
import com.chenyi.common.utils.AdminUtils;
import com.chenyi.common.utils.poi.ExcelUtil;
import com.chenyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 * 
 * @author wangkh
 */
@RestController
@RequestMapping("/admin/dict/type")
public class AdminDictTypeController extends BaseController
{
    @Autowired
    private ISysDictTypeService dictTypeService;

    @PreAuthorize("@admin.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public ResponseEntity<TableDataInfo> list(SysDictType dictType)
    {
        startPage();
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return new ResponseEntity<>(getDataTable(list),HttpStatus.OK);
    }

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PreAuthorize("@admin.hasPermi('system:dict:export')")
    @GetMapping("/export")
    public ResponseEntity<Object> export(SysDictType dictType)
    {
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        return util.exportExcel(list, "字典类型");
    }

    /**
     * 查询字典类型详细
     */
    @PreAuthorize("@admin.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public ResponseEntity<Object> getInfo(@PathVariable Long dictId)
    {
        return ResponseEntity.ok(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@admin.hasPermi('system:dict:add')")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Object> add(@Validated @RequestBody SysDictType dict)
    {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict)))
        {
            throw new MessageException("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(AdminUtils.getUsername());
        dictTypeService.insertDictType(dict);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("@admin.hasPermi('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Object> edit(@Validated @RequestBody SysDictType dict)
    {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict)))
        {
            throw new MessageException("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(AdminUtils.getUsername());
        dictTypeService.updateDictType(dict);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@admin.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public ResponseEntity<Object> remove(@PathVariable Long[] dictIds)
    {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 清空缓存
     */
    @PreAuthorize("@admin.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    public ResponseEntity<Object> clearCache()
    {
        dictTypeService.clearCache();
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public ResponseEntity<Object> optionselect()
    {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return ResponseEntity.ok(dictTypes);
    }
}
